<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2019 广东卓锐软件有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------

namespace app\api\home;

use think\Controller;
use think\exception\HttpResponseException;
use think\facade\Cache;
use think\facade\Request;
use think\Response;
use EasyWeChat\Factory;
use think\Db;
use think\Image;
use think\File;
use app\api\home\WxBizDataCrypt;
use think\Queue;
use GuzzleHttp\json_decode;

/**
 * 前台首页控制器
 * @package app\index\controller
 */
// 指定允许其他域名访问
header('Access-Control-Allow-Origin:*');
// 响应类型
header('Access-Control-Allow-Methods:*');
// 响应头设置
header('Access-Control-Allow-Headers:x-requested-with,content-type');
class Kst extends Controller
{
    protected $responseType = 'json';
    protected $pageSize = 10;
    protected $url = 'https://qiaoapi.61hc.com/';
   
    public function index(){
        $this->success('卡世通接口');
    }
    
    //获取加油卡二维码信息
    public function getQrcode(){ 
        $card=$this->request->param();
        if(!isset($card['user_token']) || !isset($card['body']) ){
            $this->error('请完善基本信息');
        }
        $header[]=$card['user_token'];
        $header[]="content-type:application/json";
        
        
        $infos=$this->qrCode($card['body'],$header);
        
        $infos=json_decode($infos,true);
        Db::name("card")->insert([
            "user_token"=>$card['user_token'],
            "body"=>$card['body'],
            "qr_code"=>$infos['qr_code'],
            "server_time"=>$infos['server_time'],
            "create_time"=>date("Y-m-d H:i:s")
        ]);
        $this->success($infos);
    }
    
    //二维码
    public function  qrCode($post_data,$header){
        set_time_limit(0);
        $url="https://b2bvcard.shell.com.cn/bhz-vcs-service/apis/v1/purchases";
        $info=$this->postUrl($url,$post_data,$header);
        return $info;
    }
    
    //查询卡余额
    public  function balance(){
        $card=$this->request->param();
        if(!isset($card['user_token']) || !isset($card['user_id'])|| !isset($card['cellphone'])){
            $this->error('请完善基本信息');
        }
        $header[]=$card['user_token'];
        $header[]="content-type:application/json";
        $url="https://b2bvcard.shell.com.cn/bhz-vcs-service/apis/v1/availablebalance?pan_code=".$card['pan_code']."&user_id=".$card['user_id']."&cellphone=".$card['cellphone']."&org_id=".$card['org_id'];
        $infos = $this->getUrl($url,$header);
        //halt($infos);
        $this->success($infos);
       
    }
    
    
    //查询交易记录
    public  function orderList(){
        $card=$this->request->param();
        if(!isset($card['user_token']) || !isset($card['pan_code'])|| !isset($card['org_id'])|| !isset($card['page'])|| !isset($card['per_page'])|| !isset($card['sort'])|| !isset($card['transcation_time_begin'])|| !isset($card['transcation_time_end'])){
            $this->error('请完善基本信息');
        }
        $header[]=$card['user_token'];
        $header[]="content-type:application/json";
        $query=[
            "transcation_type"=>"1,2,6,7",
            "is_check_display_discount"=>1,
            "transcation_time_begin"=>$card['transcation_time_begin'],
            "transcation_time_end"=>$card['transcation_time_end'],
        ];
        $query_string=json_encode($query);
        
        $url="https://b2bvcard.shell.com.cn/bhz-vcs-service/apis/v1/transcations?pan_code=".$card['pan_code']."&page=".$card['page']."&per_page=".$card['per_page']."&le_code=".$card['org_id']."&sort=&query_string=".$query_string;
        $infos = $this->getUrl($url,$header);
        //halt($infos);
        $this->success($infos);
        
    }
    
    public function postUrl($c_url, $c_url_data = false, $header = false) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$c_url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $c_url_data);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        $result = curl_exec($ch);
        curl_close ($ch);
        unset($ch);
        
        return $result;
    }
    
    
    
    
   
    

   
    /**
     * 操作成功返回的数据
     * @param string $msg   提示信息
     * @param mixed $data   要返回的数据
     * @param int   $code   错误码，默认为1
     * @param string $type  输出类型
     * @param array $header 发送的 Header 信息
     */
    protected function success($data = null,$code = 200,$msg = '',$type = null, array $header = [],$ret=null)
    {
        $this->result($data, $code, $msg, $type, $header,$ret);
    }

    /**
     * 操作失败返回的数据
     * @param string $msg   提示信息
     * @param mixed $data   要返回的数据
     * @param int   $code   错误码，默认为0
     * @param string $type  输出类型
     * @param array $header 发送的 Header 信息
     */
    protected function error($msg = '',$code = 0,$data = null, $type = null, array $header = [])
    {
        $this->result($data,$code,$msg,$type, $header);
    }

    /**
     * 返回封装后的 API 数据到客户端
     * @access protected
     * @param mixed  $msg    提示信息
     * @param mixed  $data   要返回的数据
     * @param int    $code   错误码，默认为0
     * @param string $type   输出类型，支持json/xml/jsonp
     * @param array  $header 发送的 Header 信息
     * @return void
     * @throws HttpResponseException
     */
    protected function result($data, $code = 0,$msg = '', $type = '', array $header = [],$ret=null)
    {
        $result = [
            'code' => $code,
            'message'  => $msg,
            'time' => Request::instance()->server('REQUEST_TIME'),
            'data' => $data,
            'ret' => $ret,
        ];
        // 如果未设置类型则自动判断
        $type = $type ? $type : (\request()->param(config('var_jsonp_handler')) ? 'jsonp' : $this->responseType);

        if (isset($header['statuscode']))
        {
            $code = $header['statuscode'];
            unset($header['statuscode']);
        }
        else
        {
            //未设置状态码,根据code值判断
            $code = $code >= 1000 || $code < 200 ? 200 : $code;
        }
        $response = Response::create($result, $type, $code)->header($header);
        throw new HttpResponseException($response);
    }  




   

    protected function geturl($url,$headerArray){
        //$headerArray =array("Content-type:application/json;","Accept:application/json");
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        if($headerArray){
            curl_setopt($ch,CURLOPT_HTTPHEADER,$headerArray);
        }
        
        $output = curl_exec($ch);
        curl_close($ch);
        $output = json_decode($output,true);
        return $output;
    } 
    
    
}
